/**   
 * Copyright 2011 COMERE Harold
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package etrl.shaders;

import java.util.TreeMap;

import etrl.core.DefaultShader;

/**
 * \class ShaderProgramStore
 * \brief Singleton class to manage and share shader program objects
 * @author harold
 *
 */
public class ShaderProgramStore
{
	/**
	 * The singleton instance
	 */
	private static ShaderProgramStore _instance;
	
	/**
	 * Map storing shader programs and their name
	 */
	private TreeMap <String, DynamicShaderProgram> _shaderPrograms;
	
	/**
	 * \brief Initialize the store with the default shader
	 */
	private ShaderProgramStore()
	{
		_shaderPrograms = new TreeMap <String, DynamicShaderProgram>();
		
		DefaultShader defaultShader = new DefaultShader();
		registerNewShaderProgram("DefaultShader", defaultShader);
	}
	
	/**
	 * \brief Gets the singleton instance
	 * @return Singleton instance
	 */
	public static ShaderProgramStore getInstance()
	{
		if(_instance == null)
		{
			_instance = new ShaderProgramStore();
		}
	
		return _instance;
	}
	
	/**
	 * \brief Gets a shader program from its name
	 * @param shaderProgramName Name of the shader program to get
	 * @return The shader program if it has been found, else null
	 */
	public DynamicShaderProgram getShaderProgram(String shaderProgramName)
	{
		return _shaderPrograms.get(shaderProgramName);
	}
	
	/**
	 * \brief Registers a new shader program making it available to share
	 * @param shaderProgramName Shader program name to register in
	 * @param program Shader program to register
	 */
	public void registerNewShaderProgram(String shaderProgramName, DynamicShaderProgram program)
	{
		// Check if the shader program name is already registered
		// If not
		if(_shaderPrograms.get(shaderProgramName) == null)
		{
			// Add the new entry
			_shaderPrograms.put(shaderProgramName, program);
		} 
		else // else it already exists, do nothing
			System.out.println("[ShaderProgramStore::registerNewShaderProgram] The required name is already registered.");
	}
	
}
